package com.gzc.just.play.last.war.centralserver.team;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 队伍 - 表示一个游戏队伍
 * 
 * @author AI编程助手
 * @version 1.0
 */
public class Team {
    
    /**
     * 队伍角色
     */
    public enum TeamRole {
        LEADER("队长"),        // 队伍领导者
        VICE_LEADER("副队长"), // 副队长
        MEMBER("成员");        // 普通成员
        
        private final String displayName;
        
        TeamRole(String displayName) {
            this.displayName = displayName;
        }
        
        public String getDisplayName() {
            return displayName;
        }
    }
    
    /**
     * 队伍成员
     */
    public static class TeamMember {
        public long playerId;
        public String playerName;
        public TeamRole role;
        public long joinTime;
        
        public TeamMember(long playerId, String playerName, TeamRole role) {
            this.playerId = playerId;
            this.playerName = playerName;
            this.role = role;
            this.joinTime = System.currentTimeMillis();
        }
    }
    
    private long teamId;
    private String teamName;
    private long leaderId;
    private List<TeamMember> members = new CopyOnWriteArrayList<>();
    private int maxMembers = 5; // 默认最大5个成员
    private long creationTime;
    private String description;
    private int level = 1;
    private long totalExperience = 0;
    private boolean open = true; // 是否开放加入
    
    /**
     * 构造函数
     * 
     * @param teamId 队伍ID
     * @param teamName 队伍名称
     * @param leaderId 队伍领导者ID
     * @param leaderName 队伍领导者名称
     */
    public Team(long teamId, String teamName, long leaderId, String leaderName) {
        this.teamId = teamId;
        this.teamName = teamName;
        this.leaderId = leaderId;
        this.creationTime = System.currentTimeMillis();
        
        // 添加领导者作为第一个成员
        TeamMember leader = new TeamMember(leaderId, leaderName, TeamRole.LEADER);
        members.add(leader);
    }
    
    /**
     * 添加成员
     * 
     * @param playerId 玩家ID
     * @param playerName 玩家名称
     * @param role 角色
     * @return 是否成功添加
     */
    public boolean addMember(long playerId, String playerName, TeamRole role) {
        // 检查是否已满员
        if (members.size() >= maxMembers) {
            return false;
        }
        
        // 检查是否已存在
        for (TeamMember member : members) {
            if (member.playerId == playerId) {
                return false;
            }
        }
        
        TeamMember member = new TeamMember(playerId, playerName, role);
        members.add(member);
        return true;
    }
    
    /**
     * 移除成员
     * 
     * @param playerId 玩家ID
     * @return 是否成功移除
     */
    public boolean removeMember(long playerId) {
        for (TeamMember member : members) {
            if (member.playerId == playerId) {
                members.remove(member);
                return true;
            }
        }
        return false;
    }
    
    /**
     * 更改成员角色
     * 
     * @param playerId 玩家ID
     * @param newRole 新角色
     * @return 是否成功更改
     */
    public boolean changeMemberRole(long playerId, TeamRole newRole) {
        for (TeamMember member : members) {
            if (member.playerId == playerId) {
                member.role = newRole;
                return true;
            }
        }
        return false;
    }
    
    /**
     * 获取成员
     * 
     * @param playerId 玩家ID
     * @return 队伍成员
     */
    public TeamMember getMember(long playerId) {
        for (TeamMember member : members) {
            if (member.playerId == playerId) {
                return member;
            }
        }
        return null;
    }
    
    /**
     * 获取所有成员
     * 
     * @return 成员列表
     */
    public List<TeamMember> getMembers() {
        return new ArrayList<>(members);
    }
    
    /**
     * 获取成员数量
     * 
     * @return 成员数
     */
    public int getMemberCount() {
        return members.size();
    }
    
    /**
     * 检查队伍是否满员
     * 
     * @return 是否满员
     */
    public boolean isFull() {
        return members.size() >= maxMembers;
    }
    
    /**
     * 检查是否是队伍成员
     * 
     * @param playerId 玩家ID
     * @return 是否是成员
     */
    public boolean isMember(long playerId) {
        return getMember(playerId) != null;
    }
    
    /**
     * 获取玩家在队伍中的角色
     * 
     * @param playerId 玩家ID
     * @return 角色，如果不是成员则返回null
     */
    public TeamRole getPlayerRole(long playerId) {
        TeamMember member = getMember(playerId);
        return member != null ? member.role : null;
    }
    
    /**
     * 转移队伍领导权
     * 
     * @param newLeaderId 新队长ID
     * @return 是否成功转移
     */
    public boolean transferLeadership(long newLeaderId) {
        TeamMember oldLeader = getMember(leaderId);
        TeamMember newLeader = getMember(newLeaderId);
        
        if (oldLeader != null && newLeader != null) {
            oldLeader.role = TeamRole.MEMBER;
            newLeader.role = TeamRole.LEADER;
            leaderId = newLeaderId;
            return true;
        }
        return false;
    }
    
    /**
     * 增加队伍经验
     * 
     * @param exp 经验值
     */
    public void addExperience(long exp) {
        totalExperience += exp;
        // 每10000经验升级一次（可自定义）
        int newLevel = (int)(totalExperience / 10000) + 1;
        if (newLevel > level) {
            level = newLevel;
        }
    }
    
    // Getters and Setters
    
    public long getTeamId() {
        return teamId;
    }
    
    public String getTeamName() {
        return teamName;
    }
    
    public void setTeamName(String teamName) {
        this.teamName = teamName;
    }
    
    public long getLeaderId() {
        return leaderId;
    }
    
    public long getCreationTime() {
        return creationTime;
    }
    
    public String getDescription() {
        return description;
    }
    
    public void setDescription(String description) {
        this.description = description;
    }
    
    public int getLevel() {
        return level;
    }
    
    public long getTotalExperience() {
        return totalExperience;
    }
    
    public int getMaxMembers() {
        return maxMembers;
    }
    
    public void setMaxMembers(int maxMembers) {
        this.maxMembers = Math.max(1, maxMembers);
    }
    
    public boolean isOpen() {
        return open;
    }
    
    public void setOpen(boolean open) {
        this.open = open;
    }
    
    @Override
    public String toString() {
        return String.format("Team{id=%d, name='%s', leader=%d, members=%d/%d, level=%d}",
                           teamId, teamName, leaderId, members.size(), maxMembers, level);
    }
}
